<!doctype html>
<html class="no-js fixed-layout">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>首页</title>
  <meta name="description" content="index">
  <meta name="keywords" content="index">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <link rel="icon" type="image/png" href="/static/assets/i/favicon.png">
  <link rel="apple-touch-icon-precomposed" href="/static/assets/i/app-icon72x72@2x.png">
  <meta name="apple-mobile-web-app-title" content="Amaze UI" />
  <link rel="stylesheet" href="/static/assets/css/amazeui.min.css"/>
  <link rel="stylesheet" href="/static/assets/css/admin.css">
  <style type="text/css">
    .input_width{
      width: 110px;
    }
    .pad{
      padding-top: 10px;
    }
  </style>
</head>
<body>



  <div class="admin-content">
    <div class="admin-content-body">

      <div class="am-g">


 
        <div class="am-u-sm-12 am-u-md-12 ">
          <!--中间侧块 注意下面是form表-->
          
          <div class="am-form-group">

            <div class="am-g am-margin-top am-form-group">
              <div class="am-u-sm-6 am-u-md-2" style="padding-left: 150px"><label>病害属性</label></div>
              <div class="am-u-sm-5 am-u-md-2" style="padding-left: 40px" >
                <label>病害位置</label>
              </div>

            </div>

            <div class="am-g">
              
              <div class="am-u-sm-6" id="attr_id">
              <!-- <a href="">{{ AttrInfo }}</a> -->

                {% for i in AttrInfo %}
                <div class="am-u-sm-5 am-u-md-2 am-text-right pad" >{{ i.name }}：</div>
                <div class="am-u-sm-7 am-u-md-2 am-u-end pad" >
                  <input type="text"  class="input_width" maxlength="10" attr1='{{ i.remark }}' attr2='{{ i.id }}' attr3='{{ i.name }}' > {{ i.remark }}
                
                </div>
                {% endfor %}
              </div>


              <div class="am-u-sm-6" id="position_id">
              <!-- <a href="">{{ PositionInfo }}</a> -->

                {% for i in PositionInfo %}
                {% if i.type == '1' %}
                <div class="am-u-sm-7 am-u-md-2 am-u-end pad" >
                  <select  id='position1' style="width: 80%;" attr1 = "1">
                    {% for j in i.itme %}
                    <option value="{{ j.attr_id }}">{{ j.attr_name }}</option>
                    {% endfor %}
                  </select>
                
                </div>
                {% elif i.type == '2' %}
                <div class="am-u-sm-7 am-u-md-2 am-u-end pad">
                  <select  id='position2' style="width: 80%;" onchange="position_change('position2')"  attr1 = "2">
                    {% for j in i.itme %}
                    <option value="{{ j.attr_id }}">{{ j.attr_name }}</option>
                    {% endfor %}
                  </select>
                
                </div>
                <div class="am-u-sm-5 am-u-md-2 am-u-end pad" >
                  <input type="text" id="position2_remark" class="input_width" placeholder="多参数用,分隔">
                
                </div>
                {% elif i.type == '3' %}
                <div class="am-u-sm-7 am-u-md-2 am-u-end pad" >
                  <select  id='position3' style="width: 80%;" onchange="position_change('position3')"  attr1 = "3">
                    {% for j in i.itme %}
                    <option value="{{ j.attr_id }}">{{ j.attr_name }}</option>
                    {% endfor %}
                  </select>
                
                </div>
                <div class="am-u-sm-5 am-u-md-2 am-u-end pad" >
                  <input type="text" id="position3_remark" class="input_width" placeholder="多参数用,分隔">
                
                </div>
                {% elif i.type == '4' %}
                
                <div class="am-u-sm-7 am-u-md-2 am-u-end pad" >

                  <select  id='position4'  style="width: 80%;" attr1 = "4">
                    {% for j in i.itme %}
                    <option value="{{ j.attr_id }}">{{ j.attr_name }}</option>
                    {% endfor %}
                  </select>
                
                </div>
                
                {% endif %}
                {% endfor %}
                
              </div>

            </div>

            
            <br>
            
            
          </div>
          
        </div>
        <div class="am-u-sm-12 am-u-md-6">
          <div class="am-btn-toolbar">
            <div style="padding-left:240px;">

              <button type="button" class="am-btn am-btn-primary am-btn-xs" onclick="add_submit()">确定</button>
              <button type="button" class="am-btn am-btn-primary am-btn-xs" onclick="close_window()">取消</button>


            </div>
          </div>
        </div>

      </div>
    </div>

  </div>



<!--[if lt IE 9]>
<script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdn.staticfile.org/modernizr/2.8.3/modernizr.js"></script>
<script src="assets/js/amazeui.ie8polyfill.min.js"></script>
<![endif]-->

<!--[if (gte IE 9)|!(IE)]><!-->
<script src="/static/assets/js/jquery.min.js"></script>
<!--<![endif]-->
<script src="/static/assets/js/amazeui.min.js"></script>
<script src="/static/assets/js/amazeui.tree.js"></script>
<script src="/static/assets/datatables/amazeui.datatables.min.js"></script>
<script src="/static/assets/dialog/amazeui.dialog.min.js"></script>
<link rel="stylesheet" type="text/css" href="/static/assets/css/amazeui.tree.css" />
<script src="/static/assets/js/app.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  position_change("position2");
  position_change("position3");
  read_past();
})


function read_past(){
  var data_past = parent.$("#data_past").val();
  if(data_past != ''){
    data_past = JSON.parse(data_past);
    // console.log(data_past);
    $("#attr_id input").each(function(a){
      var attr2 = $(this).attr("attr2");
      for(var i=0;i< data_past.length;i++){
        // console.log(i);
        if(data_past[i].attr_id == attr2){
          $(this).val(data_past[i].attr_value);
        }
      }
      
    });
    for(var i=0;i< data_past.length;i++){
      if(data_past[i].type < 5){
        $("#position"+data_past[i].type).val(data_past[i].attr_id);
        // console.log(data_past[i].type);
        if(data_past[i].type == 2 || data_past[i].type == 3){
          $("#position"+data_past[i].type+"_remark").val(data_past[i].attr_value);
        }
      }
    }
    

  }
  // console.log(data_past);
  // data_past=data_past.replace(/\s+/,"");
  // console.log(data_past);
  // var json = eval('(' + data_past + ')'); 
  
}









function position_change(position){
  var positionX = $("#"+position+" option:selected").text();
  if(positionX != undefined){
    var judge_id = judge(positionX);
    if(judge_id == 0){
      $("#"+position+"_remark").text("");
      $("#"+position+"_remark").attr("disabled",true);
      $("#"+position+"_remark").attr("placeholder","无参数");
    }else if (judge_id == 1){
      $("#"+position+"_remark").text("");
      $("#"+position+"_remark").attr("disabled",false);
      $("#"+position+"_remark").attr("placeholder","请输入一个参数");
    }else if(judge_id == 2){
      $("#"+position+"_remark").text("");
      $("#"+position+"_remark").attr("disabled",false);
      $("#"+position+"_remark").attr("placeholder","多参数用,分隔");
    }  
  }
}

function judge(positionX){
  var num1 = positionX.indexOf("号");
  var num2 = positionX.indexOf("m");
  var num3 = positionX.indexOf("桩号");
  var num4 = positionX.indexOf("跨");
  if(positionX == '无'){
    return 0
  }else if (num2 >= 0){
    if(num1 >=0 && num3 <0){
      return 2
    }else if((num1 >=0 && num3 >= 0) || num1 < 0){
      return 1
    }
  }else if(num4 > 0){
    return 1
  }else if(num1 >=0 && num3 <0){
    return 1
  }else if((num1 >=0 && num3 >= 0) || num1 < 0){
    return 0
  }
}

function add_submit(){
  var message ='';    //错误信息
  var message3= '';   //数量
  var message4 = '';  //病害属性
  // var attr_id = [];
  var past = [];
  $("#attr_id input").each(function(a){
    if(message != ''){
      return;
    }

    var past_new ={};
    var reg = /^[1-9]\d*$/;
    if(reg.test($(this).val()) ){
      if($(this).attr("attr3") == '数量'){
        message3 = $(this).val() + $(this).attr("attr1");
      }else{
        message4 = message4 + $(this).attr("attr3") + '：' + $(this).val() + $(this).attr("attr1") + '，';
      }
      // info[$(this).attr("attr3")]=$(this).attr("attr3") + ':' + $(this).val() + $(this).attr("attr1");
      // attr_id.push(data);
      past_new['attr_id'] = $(this).attr("attr2");
      past_new['attr_value'] = $(this).val();
      past_new['type'] = 5;
      past.push(past_new);
    }else{
      return message='请输入正确的病害属性（仅数字）！'
    }
    
  });

  // console.log(attr_id)

  var position_id = [];
  var data = {};
  $("#position_id select").each(function(a){
    if(message != ''){
      return;
    }
    
    var past_new ={};
    if($(this).attr("attr1")==1){
      // data["position"] = 1 ;
      // data["id"] = $(this).val();
      data["type_1"] = $(this).children('option:selected').text();
      // position_id.push(data);

      past_new['attr_id'] = $(this).val();
      past_new['attr_value'] = '';
      past_new['type'] = 1;
      past.push(past_new);

    }else if($(this).attr("attr1")==2){
      // data["position"] = 2 ;
      // data["id"] = $(this).val();
      var AA = group("position2");
      // console.log(AA)
      if(AA[0]==0){
        message =AA[1];
      }else{
        data["type_2"] = AA[0];

        past_new['attr_id'] = $(this).val();
        past_new['attr_value'] = $('#position2_remark').val();
        past_new['type'] = 2;
        past.push(past_new);
      }

      position_id.push(data)

    }else if($(this).attr("attr1")==3){
      // data["position"] = 3 ;
      // data["id"] = $(this).val();
      var AA = group("position3");
      // console.log(AA)
      if(AA[0]==0){
        message =AA[1];
      }else{
        data["type_3"] = AA[0];

        past_new['attr_id'] = $(this).val();
        past_new['attr_value'] = $('#position3_remark').val();
        past_new['type'] = 3;
        past.push(past_new);
      }
      // position_id.push(data)

    }else if($(this).attr("attr1")==4){
      // data["position"] = 4 ;
      // data["id"] = $(this).val();
      data["type_4"] = $(this).children('option:selected').text();
      // position_id.push(data);

      past_new['attr_id'] = $(this).val();
      past_new['attr_value'] = '';
      past_new['type'] = 4;
      past.push(past_new);
    }
  })
  if(message == ''){
    // console.log(past);
    var message1 = judge_no(data.type_1,'') + judge_no(data.type_2,'') + judge_no(data.type_3,'') ;
    var disease_name = parent.$("#disease_id option:selected").text();
    var member_name = parent.$("#bridge_member").attr("attr2");
    var message2 =member_name + '：' + judge_no(data.type_2,'，') + judge_no(data.type_3,'；')  + judge_no(data.type_1,'') + message3 + judge_no(data.type_4,'') + disease_name + '；' + message4.substring(0,message4.lastIndexOf('，'))+ '。';

    // console.log(message1);
    // JSON.stringify(past)
    // console.log(JSON.stringify(past));
    parent.print_id(JSON.stringify(past),message1,message2);
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引  
    parent.layer.close(index);//关闭弹出的子页面窗口
  }else{
    alert(message);
  }
  
}

function judge_no(a,b){
  if(a == '无' || a == undefined){
    return '';
  }else{
    return a + b
  }
}

function group(position){
  var positionX = $("#"+position+" option:selected").text();
  var judge_id = judge(positionX);
  var number = $('#'+position+'_remark').val();
  // console.log(position +judge_id)
  if(judge_id == 0){
    return [positionX,'']
  }else if(judge_id == 1){
    var reg = /^[1-9]\d*$/;
    if(reg.test(number)){
      if(positionX.indexOf("m")){
        return [positionX.replace('m',number+'m'),''];
      }else if(positionX.indexOf("跨")){
        return [positionX.replace('跨',number+'跨'),''];
      }else if(positionX.indexOf("号")){
        return [positionX.replace('号',number+'号'),''];
      }


    }else{
      // alert("请输入正确的参数！");
      return [0,'请输入正确的参数！']
      
    }
  }else if(judge_id == 2){
    var reg = /^[1-9]\d*,[1-9]\d*$/;
    if(reg.test(number)){
      var abc=number.split(',');
      positionX=positionX.replace('m',abc[1]+'m');
      positionX=positionX.replace('号',abc[0]+'号');
      return [positionX,'']
    }else{
      // alert("请输入正确的参数！");
      return [0,'请输入正确的参数！']
      
    }
  }
}





function close_window(){
  var index = parent.layer.getFrameIndex(window.name);
  parent.layer.close(index);
}

var AlertMSG=function(msg){
    AMUI.dialog.alert({ title: '错误提示', content: msg, onConfirm: function() { console.log('close'); } });
}
</script>
</body>

</html>